function loadRadQueue() {
    roomId          =   $('#radRoomId').val();
    patientTypeId   =   $('#patientType').val();
    if (roomId == null || patientTypeId == null) {
        return;
    }
    showAjaxLoader('radQueueLoaderDiv', 'radQueueActionDiv', true);
    $.ajax({
        type:    "GET",
        url:      "../RadQueue/RenderQueueAjax?set_ajax_view",
        data:     "roomId=" + roomId + "&patientType=" + patientTypeId,
        dataType: "json",
        complete: function(){
            showAjaxLoader('radQueueLoaderDiv', 'radQueueActionDiv', false);
        },
        success:    function(data) {
            if (data.error.code == 'OK') {
                $('#waitingQueueTbl tbody').html(data.result.waiting.body);
                $("#waitingQueueTbl tr:last-child").addClass('last');
                fillTotalTableData($('#waitingQueueDiv'), data.result.waiting.total_table);
                $('#inProgressQueueTbl tbody').html(data.result.in_progress.body);
                $("#inProgressQueueTbl tr:last-child").addClass('last');
                fillTotalTableData($('#inProgressQueueDiv'), data.result.in_progress.total_table);
            } else {
                displayError(data.error.mess);              
            }
        }
    });
}

function cancelRadQueueItem(patientName, radTicketId) {
    promptConfirm(_("Cancel assignment"),
            _('Are you sure to cancel assignment of patient <span class=\"impression\">{0}</span>?', patientName), function() {
        cancelRadQueueItemSubmit(radTicketId); 
        return true;
    },'YesNo','warn',_('No'));
}

function cancelRadQueueItemSubmit(radTicketId) {
    showAjaxLoader('radQueueLoaderDiv', 'radQueueActionDiv', true);
    $.ajax({
        type:    "GET",
        url:      "../RadQueue/CancelQueueItemAjax?set_ajax_view",
        data:     "radTicketId=" + radTicketId,
        dataType: "json",
        complete: function(){
            showAjaxLoader('radQueueLoaderDiv', 'radQueueActionDiv', false);
        },
        success:    function(data) {
            if (data.error.code == 'OK') { 
                loadRadQueue();
            }
            else {
                displayError(data.error.mess);              
            }
        }
    });
}

function startRadQueueItem(radTicketId) {
    showAjaxLoader('radQueueLoaderDiv', 'radQueueActionDiv', true);
    $.ajax({
        type:    "GET",
        url:      "../RadResult/StartTicketAjax?set_ajax_view",
        data:     "radTicketId=" + radTicketId,
        dataType: "json",
        complete: function(){
            showAjaxLoader('radQueueLoaderDiv', 'radQueueActionDiv', false);
        },
        success:    function(data) {
            if (data.error.code == 'OK') { 
                // User redirected to input page
                window.location =   data.result;
            }
            else {
                displayError(data.error.mess);              
            }
        }
    });
}

function finishRadQueueItem(patientName, radTicketId) {
    promptConfirm(_("Finish"),
            _('Are you sure to finish assignment of patient <span class=\"impression\">{0}</span>?', patientName), function() {
        finishRadQueueItemSubmit(radTicketId); 
        return true;
    },'YesNo','warn',_('No'));
}

function finishRadQueueItemSubmit(radTicketId) {
    showAjaxLoader('radQueueLoaderDiv', 'radQueueActionDiv', true);
    $.ajax({
        type:    "GET",
        url:      "../RadQueue/FinishQueueItemAjax?set_ajax_view",
        data:     "radTicketId=" + radTicketId,
        dataType: "json",
        complete: function(){
            showAjaxLoader('radQueueLoaderDiv', 'radQueueActionDiv', false);
        },
        success:    function(data) {
            if (data.error.code == 'OK') {
                loadRadQueue();
            }
            else {
                displayError(data.error.mess);              
            }
        }
    });
}

function fillTotalTableData(parentDiv, data) {
	$('table.total_table', parentDiv).remove();
	parentDiv.append(data);
	parentDiv.children('table:last-child').addClass('total_table');
}